limsLocationBoard.sampleStorageBoard2ChoosePage = {

    getStorageSampleListColumns: function () {
        return [{
            checkbox: true
        }, {
            field: 'sampleCode',
            title: 'T_LIMS_SAMPLE.SAMPLECODE',
            styleFormatter: function (row) {
            },
        }, {
            field: 'locations',
            title: 'T_LIMS_SAMPLE.LOCATIONS2',
            editor: true,
            type: 'choose',
            category: 'board-choose',
            single: true,
            onBeforeChoose: function () {
                Gikam.alert('T_LIMS_LOCATION.TIP.NOT_CHOOSE');
                return false;
            },
            onClean: function (field, value, index) {
                var _this = this;
                let activedRow = this.getData()[index];
                workspace.window.showMask();
                Gikam.postText(limsLocationBoard.baseUrl + "/clean-samples", Gikam.getJsonWrapper({}, [null, [activedRow]])).done(function (data) {
                    _this.refreshRowById(activedRow.id);
                    const iframeDom = document.querySelector("#ksh-storage-location-board2-iframe >iframe");
                    iframeDom.contentWindow.postMessage({
                        type: 1
                    }, "*");
                }).always(function () {
                    workspace.window.closeMask();
                });
            },
            width: 120
        }, {
            field: 'ext$.batchcode',
            title: 'T_LIMS_BATCH.BATCHCODE',
            type: 'link',
            width: 120
        }, {
            field: 'receiveAbnormalType',
            title: 'T_LIMS_SAMPLE.RECEIVEABNOMALTYPE',
            type: 'select',
            firstBlank: false,
            multiple: true,
            category: 'sys',
            param: {
                codeCategoryId_EQ: 'receiveAbnormalType'
            },
        }, {
            field: 'collectTime',
            title: 'T_LIMS_SAMPLE.COLLECTTIME',
            preInsert: true,
            type: 'dateTime',
            width: 140
        }];
    },

    getSampleGrid: function () {
        var _this = this;
        var requestData = _this.param;
        return {
            type: 'grid',
            id: 'lims-storage-location-board2-sample-list-grid',
            url: limsLocationBoard.sampleUrl + '/queries/rows',
            requestData: requestData,
            page: true,
            columns: _this.getStorageSampleListColumns(),
            onCellClick: function (field, row) {
                field === 'ext$.batchcode' && row.ext$.batchid && limsLocationBoard.loadBatchDetailPage(row)
            },
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                }
            },
            toolbar: [{
                type: 'form',
                id: 'sample-storage2-form',
                onRendered: function () {
                    this.hideFields(['boardId']);
                },
                titleWidth: 30,
                fields: [{
                    field: 'assignType',
                    title: 'T_LIMS_LOCATION.ASSIGNTYPE',
                    refreshGrid: false,
                    inputWidth: 80,
                    type: 'select',
                    firstBlank: false,
                    category: 'sys',
                    param: {
                        codeCategoryId: 'assignType'
                    },
                    value: 'x',
                    onChange: function (field, value) {
                        this.setData({
                            startHole: ''
                        })
                    }
                }, {
                    field: 'startHole',
                    title: 'T_LIMS_LOCATION.STARTHOLE',
                    refreshGrid: false,
                    inputWidth: 80,
                    placeholder: '格式为一位字母加数字，例：A01'// 未生效
                }, {
                    field: 'boardId',
                    title: 'boardID',
                    refreshGrid: false
                }]
            }, {
                type: 'dropDownMenu',
                text: 'T_LIMS_LOCATION.TITLE.LOCATION',
                icon: 'select',
                items: [{
                    type: 'button',
                    text: 'LIMS.LOCATION.BATCH.ASSIGN_LOCATION',
                    icon: 'modify',
                    onClick: function () {
                        _this.batchAssignLocation();
                    }
                }, {
                    type: 'button',
                    text: 'LIMS.LOCATION.BATCH.CLEAN_LOCATION',
                    icon: 'remove-row',
                    onClick: function () {
                        _this.batchClearLocation();
                    }
                }]
            }]
        }
    },

    batchAssignLocation: function () {
        var _this = this;
        let data = Gikam.getComp('sample-storage2-form').getData();
        if (Gikam.isEmpty(data.boardId) || data.boardId === '1') {
            Gikam.alert('请先选中板子！');
            return;
        }
        var grid = Gikam.getComp('lims-storage-location-board2-sample-list-grid');
        var selections = grid.getSelections();
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
            return;
        }
        var res = Gikam.postSync(limsLocationBoard.baseUrl + '/queries', Gikam.getJsonWrapper({
            f: {
                id_EQ: data.boardId
            },
            n: -1,
            s: -1
        }));
        var boardData = res.responseJSON.rows[0];
        if (boardData.freeHoleCount < selections.length) {
            Gikam.alert('GIKAM.TIP.BOARD_NOT_ADEQUATE')
            return
        }
        var flag = false;
        for (var i = 0; i < selections.length; i++) {
            if (!Gikam.isEmpty(selections[i].locations)) {
                flag = true;
                break;
            }
        }
        if (flag) {
            Gikam.alert('GIKAM.TIP.SAMPLE_NO_LOCATION');
            return;
        }

        let startHole = data.startHole;
        let row = 1;
        let col = 1;
        if (!Gikam.isEmpty(startHole)) {
            // 校验孔位是否满足格式
            let regex = "^[a-zA-Z][0-9]+$";
            if (!startHole.match(regex)) {
                Gikam.alert('起始孔位须为【一位字母+数字】的格式，例：【A01】');
                return;
            }
            // 校验输入孔位是否正确
            let rowNum = boardData.rowNum;
            let columnNum = boardData.columnNum;
            row = startHole.toUpperCase().charCodeAt(0) - 64;
            col = startHole.substring(1).replace(/\b(0+)/gi, "");
            if (row > rowNum || parseInt(col) > columnNum) {
                Gikam.alert('输入孔位在板中不存在');
                return;
            }
        }

        let assignType = Gikam.getComp('sample-storage2-form').getData().assignType;

        let bool = selections.filter(s => !Gikam.isEmpty(s.boardId) || !Gikam.isEmpty(s.locationId));
        if (bool.length > 0) {
            Gikam.alert('T_LIMS_LOCATION_BOARD.ASSIGN.SAMPLE_ASSIGNABLE');
            return;
        }

        workspace.window.showMask();
        Gikam.postText(limsLocationBoard.baseUrl + '/update-samples-batch',
            Gikam.getJsonWrapper({
                assignType: assignType,
                row: row,
                col: col,
                storageType: boardData.ext$.storagetype
            }, [null, [boardData]], [null, selections])).done(function (message) {
            if (message !== 'success') {
                Gikam.alert(message);
                return;
            }
            Gikam.getComp('lims-storage-location-board2-sample-list-grid').refresh();
            _this.refreshHole();
        }).always(function () {
            let searchForm = Gikam.getComp('sample-storage2-form');
            searchForm.setData({
                // assignType: 'x',
                sampleCode: '',
            })// 分配位置成功后清除左上表格上面搜索框的内容
            workspace.window.closeMask();
        });
    },

    batchClearLocation: function () {
        var _this = this;
        var grid = Gikam.getComp('lims-storage-location-board2-sample-list-grid');
        var selections = grid.getSelections();
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
            return;
        }
        Gikam.confirm('GIKAM.BUTTON.CONFIRM', Gikam.propI18N('LIMS.LOCATION.BUTTON.BATCH.CLEAN_LOCATION'), function () {
            workspace.window.showMask();
            Gikam.postText(limsLocationBoard.baseUrl + "/clean-samples", Gikam.getJsonWrapper({}, [null, selections])).done(function (data) {
                grid.refresh();
                _this.refreshHole();
            }).always(function () {
                workspace.window.closeMask();
            });
        });
    },

    refreshHole: function () {
        const iframeDom = document.querySelector("#ksh-storage-location-board2-iframe >iframe");
        iframeDom.contentWindow.postMessage({
            type: 1
        }, "*");
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-storage-location-sample-board2-choose-list-layout',
            renderTo: Gikam.getLastModal().window.$dom,
            west: {
                title: 'LIMS.TAB.LOCATION.BOARD',
                width: '30%',
                items: [this.getSampleGrid()]
            },
            center: {
                items: [{
                    type: 'iframe',
                    src: '/static/basemodule/samplemanage/ksh-assign-location/index.html',
                    id: 'ksh-storage-location-board2-iframe',
                    scrolling: 'yes'
                }]
            }
        });
    },

    init: function (param) {
        this.param = param;
        this.create();
    }
};
